//
//  HDStatusCell.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/13.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit
import SnapKit
class HDStatusCell: UITableViewCell {
//设置底部视图的顶部约束
var  bottomViewTopConstraint :Constraint?
//模型数组属性
    var status:HDStatus?{
        didSet{
            originalView.statuses = status
            //判断是否转发模型
            //卸载约束
            bottomViewTopConstraint?.uninstall()
            //如果有转发模型  赋值 和重写设置bottomView的顶部约束
            if let  retweetSatus = status?.retweeted_status{
                    reweetView.hidden  = false
                    reweetView.status = retweetSatus
                    bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopConstraint = make.top.equalTo(reweetView.snp_bottom).constraint
                })
            }else{//没有转发微博
                  reweetView.hidden = true
                    bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }
        }
    }

//MARK  初始化方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
         super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置背景颜色
        self.contentView.backgroundColor  = UIColor(white: 237/255.0, alpha: 1)
        //创建子控件 并添加约束
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   ///创建子控件 并添加约束
    private func prepareUI(){
        //添加子控件到父容器
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(reweetView)
        //添加 originalView约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(cellMargin)
        }
        //添加 reweetView的约束
        reweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing .equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        //添加 bottomView的约束
        bottomView.snp_makeConstraints { (make) -> Void in
               make.leading.trailing.equalTo(contentView)
               make.top.equalTo(reweetView.snp_bottom)
               make.height.equalTo( HDbottomViewHeight)
            //记录约束
               bottomViewTopConstraint = make.top.equalTo(reweetView.snp_bottom).constraint
        }

        //添加contentView约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
        //异步绘制图层
        self.layer.drawsAsynchronously = true
        //删栏化
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }
    
//MARK -懒加载控件
    private lazy var  originalView:HDStatusOriginlView = HDStatusOriginlView()
    private lazy var  bottomView:HDBottomView = HDBottomView()
    private lazy var  reweetView:HDStatusRetweedView = HDStatusRetweedView()
    
  

}
